package com.bawei.mdm2207alast.mine.ask

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.R
import com.bawei.mdm2207alast.base.OnError
import com.bawei.mdm2207alast.base.OnLoading
import com.bawei.mdm2207alast.base.OnSuccess
import com.bawei.mdm2207alast.common.BaseTitle
import com.bawei.mdm2207alast.model.db.AskEntity
import com.bawei.mdm2207alast.route.RouteConfig
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

/**
 * 作者：赵亮
 * 时间：2024/12/24 11:06
 * 功能：员工请假列表
 */
@Composable
fun AskListPage(
    navController: NavController = rememberNavController(),
    viewModel: AskViewModel = hiltViewModel()
) {

    // 1. 数据
    val list = remember {
        SnapshotStateList<AskEntity>()
    }
    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when (it) {
                    is OnError -> {
                        ToastUtils.showShort("查询失败")
                    }

                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as Collection<AskEntity>)
                    }
                }
            }
        }
        launch {
            viewModel.sendIntent(AskIntent.SelAsk(MMKV.defaultMMKV().decodeLong("uid")))
        }
    }

    Column {
        Box {
            BaseTitle(navController = navController, title = "申请列表")
            Text(
                text = "申请",
                modifier = Modifier
                    .padding(top = 24.dp, end = 15.dp)
                    .align(Alignment.CenterEnd)
                    .clickable {
                        // 跳转到请假申请页面
                        navController.navigate(RouteConfig.ASK_COMMIT_PAGE)
                    }
            )
        }

        Column(modifier = Modifier.padding(start = 15.dp, end = 15.dp, top = 10.dp)) {
            // 表头
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .background(color = colorResource(id = R.color.ask_list_title_bg)),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(text = "类型", modifier = Modifier.padding(start = 15.dp))
                Text(text = "开始时间")
                Text(text = "结束时间")
                Text(text = "状态", modifier = Modifier.padding(end = 15.dp))
            }

            LazyColumn(
                modifier = Modifier.padding(top = 10.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                items(list) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.SpaceBetween,
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(50.dp)
                            .clickable {
                                // 跳转到请假详情
                                navController.navigate(RouteConfig.ASK_DETAIL + "/${Gson().toJson(it)}")
                            }
                            .background(
                                color = colorResource(
                                    id = R.color.add_report_bg
                                ), shape = RoundedCornerShape(10.dp)
                            )
                    ) {
                        Text(text = it.type, modifier = Modifier.padding(start = 15.dp))
                        Text(text = it.start)
                        Text(text = it.end)
                        var color = Color.Gray
                        if (it.status == "已提交") {
                            color = Color.Gray
                        } else if (it.status == "同意") {
                            color = Color.Green
                        } else {
                            color = Color.Red
                        }
                        Box(
                            modifier = Modifier
                                .padding(horizontal = 10.dp, vertical = 5.dp)
                                .background(color = color, shape = RoundedCornerShape(10.dp))
                        ) {
                            Text(
                                text = it.status,
                                modifier = Modifier
                                    .padding(end = 15.dp)
                            )
                        }
                    }
                }
            }
        }


    }

}

@Preview(showSystemUi = true)
@Composable
fun AskListPagePreview() {
    AskListPage()
}

